package cn.bs.busuo.content.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.content.dao.persist.repository.SightArticleRepository;
import cn.bs.busuo.content.pojo.entity.ContentSightArticle;
import cn.bs.busuo.content.pojo.entity.ContentSightArticleDetail;
import cn.bs.busuo.content.pojo.param.SightArticleAddNewParam;
import cn.bs.busuo.content.pojo.vo.ContentSightArticleListBySortVO;
import cn.bs.busuo.content.service.SightArticleService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Feng
 * @since: 2023/12/7
 */
@Service
public class SightArticleServiceImpl implements SightArticleService {

    @Autowired
    private SightArticleRepository sightArticle;

    //发布文章
    @Override
    public void addNewSightArticle(CurrentPrincipal currentPrincipal, SightArticleAddNewParam sightArticleAddNewParam) {
        //插入到酒店文章
        ContentSightArticle contentSightArticle = new ContentSightArticle();
        contentSightArticle.setBrief(sightArticleAddNewParam.getBrief());
        contentSightArticle.setCoverUrl(sightArticleAddNewParam.getCoverUrl());
        contentSightArticle.setSightsId(sightArticleAddNewParam.getSightsId());
        contentSightArticle.setKeywords(sightArticleAddNewParam.getKeywords());
        contentSightArticle.setTitle(sightArticleAddNewParam.getTitle());

        //插入当事人信息
        contentSightArticle.setAuthorId(currentPrincipal.getId());
        contentSightArticle.setAuthorName(currentPrincipal.getUsername());

        //插入到景点酒店详情实体类
        ContentSightArticleDetail contentSightArticleDetail = new ContentSightArticleDetail();
        contentSightArticleDetail.setDetail(sightArticleAddNewParam.getDetail());

        //调用添加方法
        addSlightArticle(contentSightArticleDetail,contentSightArticle);
    }

    //删除文章
    @Override
    public void deleteArticle(List<Long> ids) {
        deleteSightArticle(ids);
    }

    //推荐文章
    @Override
    public void setIsRecommendSightArticle(List<Long> ids) {
        for (Long id : ids){
            if (sightArticle.selectSightArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有id为: " + id + "这篇文章");
            }
            if (sightArticle.contentSightArticleById(id).getIsRecommend() == 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是推荐文章");
            }
            if (sightArticle.contentSightArticleById(id).getCheckState()!= 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"文章未审核");
            }
        }
        int i = sightArticle.updateIsRecommend(ids, 1);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"推荐文章失败,服务器繁忙,请稍后再试");
        }

    }

    //不推荐文章
    @Override
    public void setIsNotRecommendSightArticle(List<Long> ids) {
        for (Long id : ids){
            if (sightArticle.selectSightArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if(sightArticle.contentSightArticleById(id).getIsRecommend() == 2){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是不推荐文章");
            }
        }
        int i = sightArticle.updateIsNotRecommend(ids, 0);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"取消推荐文章失败,服务器繁忙,请稍后再试");
        }
    }

    //审核通过
    @Override
    public void setCheckStatePass(List<Long> ids) {
        for (Long id : ids){
            if (sightArticle.selectSightArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if (sightArticle.contentSightArticleById(id).getCheckState() == 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经审核通过");
            }
        }
        int i = sightArticle.updateCheckState(ids, 1);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"审核文章失败,服务器繁忙,请稍后再试");
        }

    }

    //审核为不通过
    @Override
    public void setCheckStateNotPass(List<Long> ids) {
        for (Long id : ids){
            if (sightArticle.selectSightArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if((sightArticle.contentSightArticleById(id).getCheckState() == 2)){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该文章已审核");
            }
        }

        int i = sightArticle.updateCheckNotState(ids, 2);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"审核文章失败,服务器繁忙,请稍后再试");
        }

    }

    //设置排序
    @Override
    public void setSort(Long id,Integer sort) {
        ContentSightArticle contentSightArticle = sightArticle.contentSightArticleById(id);
        //判断文章是否存在
        if (contentSightArticle == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        //判断是否通过审核
        if ((contentSightArticle.getCheckState() == 0) || (contentSightArticle.getCheckState() == 2)){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该文章还未通过审核");
        }
        //判断设置的排序与数据库里面的是否重复
        List<ContentSightArticleListBySortVO> contentSightArticlesList = sightArticle.selectListBySort();
        for (ContentSightArticleListBySortVO contentSightArticleListBySortVO : contentSightArticlesList) {
            if (contentSightArticleListBySortVO.getSort() == sort){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经存在该排序");
            }

        }
        int i = sightArticle.updateSort(id,sort);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"设置排序失败,服务器繁忙,请稍后再试");
        }
    }

    //查看排序文章
    @Override
    public String listSortArticle() {
        List<ContentSightArticleListBySortVO> contentSightArticlesList = sightArticle.selectListBySort();
        String json = JSON.toJSON(contentSightArticlesList).toString();
        return json;
    }

    //查看未审核文章
    @Override
    public String listCheckStateArticle() {
        List<ContentSightArticle> contentSightArticlesList = sightArticle.selectListByCheckState();
        if (contentSightArticlesList.size() == 0){
            throw new ServiceException(ServiceCode.OK,"暂时还没有未审核的景点文章");
        }
        String json = JSON.toJSON(contentSightArticlesList).toString();
        return json;
    }





    //添加景点文章IO
    @Transactional
    public void addSlightArticle(ContentSightArticleDetail contentSightArticleDetail, ContentSightArticle contentSightArticle) {
        //判断该文章标题是否重复
        int title = sightArticle.contentSightArticleByTitle(contentSightArticle.getTitle());
        if (title != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        //添加到景点文章
        int i = sightArticle.addNewSightArticle(contentSightArticle);
        if (i != 1){
            String msg = "发布景点文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }

        //从景点文章里面获取文章Id
        Long NewArticleId = sightArticle.selectSightArticleByTitle(contentSightArticle.getTitle());
        contentSightArticleDetail.setSightArticleId(NewArticleId);

        //添加到景点文章详情
        int j = sightArticle.addNewSightArticleDetail(contentSightArticleDetail);
        if (j != 1){
            String msg = "发布景点文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }


    //------------工具方法


    //删除景点文章IO
    @Transactional
    public void deleteSightArticle(List<Long> ids) {
        for (Long id : ids){
            if (sightArticle.selectSightArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
        }
//        //判断该文章是否存在
//        int i = sightArticle.selectSightArticleById(id);
//        if (i == 0){
//            String msg = "该文章不存在";
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
//        }

        //删除景点文章
        int j = sightArticle.deleteSightArticle(ids);
        if (j == 0){
            String msg = "删除景点文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_DELETE,msg);
        }

    }
}
